package Leetcode.lc003;

import java.util.HashMap;
import java.util.Map;

/**
 * 滑动窗口，无重复的最长字串
 */
public class Sloution {
    /**
     *
     * @param s
     * @return
     */
    public int lengthOfLongestSubstring(String s) {
        int left=0,result=0;
        HashMap<Character, Integer> map = new HashMap<>();
        for(int i=0;i<s.length();i++){
            if(map.containsKey(s.charAt(i))){
                left=Math.max(left,map.get(s.charAt(i))+1);
            }
            map.put(s.charAt(i),i);
            result=Math.max(result,i-left+1);
        }
        return result;
    }

    public static void main(String[] args) {
        Sloution sloution = new Sloution();
        String s="abcabcbb";
        int a=sloution.lengthOfLongestSubstring(s);
        System.out.println(a);
    }
}
